package View.GeneralManagerView.VariablePanel;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class VariablePane extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static final int height = 21;
	Addins contains;
	
	
	public VariablePane(int width,int height){
		super();
		setSize(width,height);
	}
	
	public VariablePane(int width,int height,String...args) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		super();
		setSize(width,height);
		addItems(args);
		init();
	}
	
	public VariablePane(String...args) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		super();
		addItems(args);
		init();
	}
	
	public VariablePane(Addins c){
		super();
		this.contains = c;
		init();
	}
	
	private void init(){
		setLayout(null);
		if(getHeight()<this.contains.getLeastheight())
			setSize(getWidth(), this.contains.getLeastheight());
		
		this.contains.setPosition(getWidth(), getHeight());
		placeItems();
	}
	
	public void addItems(String...args) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		contains = new Addins(args);
		init();
	}
		
	private void placeItems(){
		for(int i=0;i<contains.size;i++){
			add(contains.name[i]);
			add((Component) contains.inputBox[i]);
		}
	}
	
	public void setSize(int width,int height){
		super.setSize(width,height);
		super.setPreferredSize(new Dimension(width, height));
		if(contains!=null)
			contains.setPosition(width, height);
	}
	
	public static void main(String[] text){
		JFrame jk = new JFrame();
		jk.setBounds(100,100,400,700);
		VariablePane vpTest = null;
		try {
			vpTest = new VariablePane("Input1","textInputField","Input2","textInputField","Input3","textInputField","Input4","textInputField");
		} catch (InstantiationException | IllegalAccessException
				| ClassNotFoundException e) {
			e.printStackTrace();
		}
		vpTest.setSize(300, 600);
		jk.add(vpTest);
		jk.setVisible(true);
	}
	
	public Object[] getInput(){
		return contains.getInput();
	}
}




class Addins{
	
	static final int height = 21;
	static final int tabula = 10;
	static final int widthPerWord = 20;
	static final int defaultWidthOfInput = 100;
	static final String DefaultClassPath = Addins.class.getPackage().getName();
	
	JLabel name[] = null;
	InputField inputBox[] = null;
	public int size = 0;
	
	
	public Addins(String...args) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		
		if(args.length%2==1)
			throw new RuntimeException();
		else{
			name = new JLabel[args.length/2];
			inputBox = new InputField[args.length/2];
			
			for(int i=0;i<args.length;i+=2){
				name[i/2] = new JLabel(args[i]);
				name[i/2].setSize(args[i].length()*widthPerWord, height);
				name[i/2].setHorizontalAlignment(JLabel.RIGHT);
				Class<?> d = java.lang.ClassLoader.getSystemClassLoader().loadClass(DefaultClassPath+"."+args[i+1]);
				Class<?> ports[] = d.getInterfaces();
				boolean hasinterface = false;
				for(int j=0;j<ports.length;j++){

					if(InputField.class.isAssignableFrom(ports[j])){
						hasinterface = true;
						break;
					}
				}
				if(!hasinterface)
					throw new RuntimeException();
				
				inputBox[i/2] = (InputField) d.newInstance();
				inputBox[i/2].setSize(defaultWidthOfInput, height);
				size++;
			}
		}
		
	}
	
	public boolean hasInputInterface(Class<?> t){
		 Class<?> superClass=t.getSuperclass();
		 if(superClass==null)
			 return false;
		 else if(superClass==InputField.class)
			 return true;
		 else
			 return hasInputInterface(superClass);
	}
	
	public Addins(){
		name = new JLabel[10];
	}
	
	@SuppressWarnings("static-access")
	public void setPosition(int width,int height){
		int gap = (height - size*this.height)/(size+1);
		
		
		for(int i=0;i<size;i++){
			name[i].setLocation(width/2-name[i].getWidth()-tabula,gap*(i+1)+this.height*i);
			inputBox[i].setLocation(width/2+tabula,gap*(i+1)+this.height*i);
		}
	}
	
	public Object[] getInput(){
		Object inputs[] = new Object[size];
		for(int i=0;i<size;i++){
			inputs[i] = inputBox[i].getFillingIn();
		}
		return inputs;
	}
	
	public int getLeastheight(){
		return (size+1)*31;
	}
	
	public void addItem(String...args) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		Addins tmp = new Addins(args);
		JLabel newName[] = new JLabel[size+tmp.size];
		InputField newInputBox[] = new InputField[size+tmp.size];
		for(int i=0;i<size;i++){
			newName[i] = this.name[i];
			newInputBox[i] = this.inputBox[i];
		}
		
		for(int i=0;i<tmp.size;i++){
			newName[i+size] = tmp.name[i];
			newInputBox[i+size] = tmp.inputBox[i];
		}
		
		this.name = newName;
		this.inputBox = newInputBox;
		this.size = size+tmp.size;

	}
	
}